#!/user/bin/env jython
# -*- coding: utf-8 -*-

from javax.swing import *
from java.awt import *
from java.lang import *
import JDataAccess
from JHTMLGenerator import *
import os
import time
from JCSVGenerator import *


class JDefaultQueries:
	
	def __init__(self, panel, frame, statusBarInfo):
		
		self.frame = frame
		self.mainPanel = panel
		self.statusBarInfo = statusBarInfo
		
		self.comboList1 = []
		self.comboList2 = []
		self.comboList3 = []
		
		self.htmlModule = JHTMLGenerator()	
		self.csvGenerator = JCsv()
		
		self.makeTopPanel()
		self.addLabelToTop()
		self.makeMidlePanel()
		self.fill_treeQueries()
		
		print "ola"
		
		pass
		
	def makeTopPanel(self):
		
		self.topPanel = JPanel()
		self.topPanel.setPreferredSize(Dimension(800,50))
		self.topPanel.setMinimumSize(Dimension(800,50))
		self.topPanel.setMaximumSize(Dimension(800,50))
		self.topPanel.setBackground(Color.ORANGE)
		self.topPanel.layout = FlowLayout(FlowLayout.CENTER)
		self.mainPanel.add(self.topPanel)
		
	def addLabelToTop(self):
		
		topTitle = JLabel()
		topTitle.setText("Default Queries")
		topTitle.setFont(Font("Arial", Font.BOLD, 32));
		self.topPanel.add(topTitle)
		
	def makeMidlePanel(self):
		
		auxPanel1 = JPanel()
		auxPanel1.setMaximumSize(Dimension(800, 100))
		auxPanel1.setPreferredSize(Dimension(800, 100))
		self.mainPanel.add(auxPanel1)
		################################################
		self.midlePanel = JPanel()
		self.midlePanel.layout = BoxLayout(self.midlePanel, BoxLayout.Y_AXIS)
		self.midlePanel.setMaximumSize(Dimension(800, 300))
		self.mainPanel.add(self.midlePanel)
		
	def fill_treeQueries(self):
		
		queryPanel1 = JPanel()
		queryPanel1.setMaximumSize(Dimension(800, 50))
		queryPanel1.layout = FlowLayout(FlowLayout.LEFT)
		
		queryTitle1 = JLabel()
		queryTitle1.setText("number of teachers in the higher education" + 
		" system per establishment per year")
		
		queryTitle1.setFont(Font("Arial", Font.BOLD, 15));
		
		queryButton1 = JButton("Executar", actionPerformed = self.firstQueryEvent)
		
		queryPanel1.add(queryTitle1)
		queryPanel1.add(queryButton1)
		self.midlePanel.add(queryPanel1)
		
		#####################################
		
		queryPanel2 = JPanel()
		queryPanel2.setMaximumSize(Dimension(800, 50))
		queryPanel2.layout = FlowLayout(FlowLayout.LEFT)
		
		queryTitle2 = JLabel()
		queryTitle2.setText("number of teachers divided per categories" +
		" per establisment and per year")
		
		queryTitle2.setFont(Font("Arial", Font.BOLD, 15));
		
		queryButton2 = JButton("Executar", actionPerformed = self.secondQueryEvent)
		
		queryPanel2.add(queryTitle2)
		queryPanel2.add(queryButton2)
		self.midlePanel.add(queryPanel2)
		
		##########################################
		
		queryPanel3 = JPanel()
		queryPanel3.setMaximumSize(Dimension(800, 50))
		queryPanel3.layout = FlowLayout(FlowLayout.LEFT)
		
		queryTitle3 = JLabel()
		queryTitle3.setText("<html><justify>number of teachers divided" + 
		" per type of degree<br> (doctorate level, masters level and graduate level)" + 
		" per establishment and per year</justify></html>")

		
		queryTitle3.setFont(Font("Arial", Font.BOLD, 15));
		
		queryButton3 = JButton("Executar", actionPerformed = self.thirdQueryEvent)
		
		queryPanel3.add(queryTitle3)
		queryPanel3.add(queryButton3)
		self.midlePanel.add(queryPanel3)
		

		
	def firstQuery(self):
		
				
		queryTitle1 = JLabel()
		queryTitle1.setText("Search number of teachers in:")
		queryTitle1.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel1_1 = JPanel()
		subPanel1_1.setMaximumSize(Dimension(300, 50))
		subPanel1_1.add(queryTitle1)
		
		self.midlePanel.add(subPanel1_1)
		
		#################################################
		comboBox1 = JComboBox(self.listElementsCombo1)
		comboBox1.setPreferredSize(Dimension(600, 30))
		
		queryTitle2 = JLabel()
		queryTitle2.setText("Establishment:")
		queryTitle2.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel2 = JPanel()
		subPanel2.layout = FlowLayout(FlowLayout.LEFT)
		subPanel2.setMaximumSize(Dimension(800, 50))
		
		subPanel2.add(queryTitle2)
		subPanel2.add(comboBox1)
		
		self.midlePanel.add(subPanel2)
		self.comboList1.append(comboBox1)
		#################################################
		comboBox2 = JComboBox(self.listElementsCombo2)
		comboBox2.setPreferredSize(Dimension(600, 30))
		
		queryTitle3 = JLabel()
		queryTitle3.setText("Year:")
		queryTitle3.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel3 = JPanel()
		subPanel3.layout = FlowLayout(FlowLayout.LEFT)
		subPanel3.setMaximumSize(Dimension(800, 50))
		
		subPanel3.add(queryTitle3)
		subPanel3.add(comboBox2)
		
		self.midlePanel.add(subPanel3)
		self.comboList1.append(comboBox2)
		
		##################################################
		subPanel4 = JPanel()
		subPanel4.layout = FlowLayout(FlowLayout.CENTER)
		subPanel4.setMaximumSize(Dimension(800, 50))
		
		queryButton1 = JButton("Execute Query", actionPerformed = self.firstQueryExecuteEvent)
		queryButton2 = JButton("Generate html", actionPerformed = self.firstQueryExecuteHTMLEvent)
		queryButton3 = JButton("CSV Generator", actionPerformed = self.firstQueryExecuteCsv)
		
		subPanel4.add(queryButton1)
		subPanel4.add(queryButton2)
		subPanel4.add(queryButton3)
		self.midlePanel.add(subPanel4)
		#################################################
		
		subPanel5 = JPanel()
		subPanel5.layout = FlowLayout(FlowLayout.LEFT)
		subPanel5.setMaximumSize(Dimension(800, 50))
		
		queryResult1 = JLabel()
		queryResult1.setText("Result: ")
		queryResult1.setFont(Font("Arial", Font.BOLD, 15));
		
		self.queryResult2 = JLabel()
		self.queryResult2.setText("")
		self.queryResult2.setFont(Font("Arial", Font.BOLD, 15));
		
		
		subPanel5.add(queryResult1)
		subPanel5.add(self.queryResult2)
		self.midlePanel.add(subPanel5)
	
	
	def firstQueryEvent(self, event):
		
		self.midlePanel.removeAll()
		
		self.statusBarInfo.setText("Please wait as information is loaded...")
		
		self.frame.repaint()
		self.frame.setVisible(True)	
		
		JOptionPane.showMessageDialog(self.frame,"The Program need to load some data...")
							
		self.listElementsCombo1 = JDataAccess.getListEstablishment()
		self.listElementsCombo2 = JDataAccess.getListYears()
		#self.listElementsCombo3 = JDataAccess.getListDegrees()
						
		self.firstQuery()
		
		self.frame.repaint()
		self.frame.setVisible(True)
		self.statusBarInfo.setText("Ready!")
		
	def firstQueryExecuteEvent(self, event):
		
				
		self.listResult = JDataAccess.getFirstDefaultQuery(self.comboList1[0].getSelectedItem(), self.comboList1[1].getSelectedItem())
		self.queryResult2.setText(str(len(self.listResult)))
	
	def firstQueryExecuteHTMLEvent(self, event):
		
		listResultHtml = JDataAccess.getFirstDefaultQuery(self.comboList1[0].getSelectedItem(), self.comboList1[1].getSelectedItem())
		self.htmlModule.firstQuery(listResultHtml, self.comboList1[0].getSelectedItem(), self.comboList1[1].getSelectedItem())
	
	def firstQueryExecuteCsv(self, event):
		
		listResult = JDataAccess.getFirstDefaultQuery(self.comboList1[0].getSelectedItem(), self.comboList1[1].getSelectedItem())
		self.csvGenerator.firstQuery(listResult, len(listResult), self.comboList1[0].getSelectedItem(), self.comboList1[1].getSelectedItem())
		
		print "csv"
		
	
	def secondQuery(self):
		
				
		queryTitle1 = JLabel()
		queryTitle1.setText("Search number of teachers in:")
		queryTitle1.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel1_1 = JPanel()
		subPanel1_1.setMaximumSize(Dimension(300, 50))
		subPanel1_1.add(queryTitle1)
		
		self.midlePanel.add(subPanel1_1)
		
		#################################################
		comboBox1 = JComboBox(self.listElementsCombo1)
		comboBox1.setPreferredSize(Dimension(600, 30))
		
		queryTitle2 = JLabel()
		queryTitle2.setText("Establishment:")
		queryTitle2.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel2 = JPanel()
		subPanel2.layout = FlowLayout(FlowLayout.LEFT)
		subPanel2.setMaximumSize(Dimension(800, 50))
		
		subPanel2.add(queryTitle2)
		subPanel2.add(comboBox1)
		
		self.midlePanel.add(subPanel2)
		self.comboList2.append(comboBox1)
		#################################################
		comboBox2 = JComboBox(self.listElementsCombo2)
		comboBox2.setPreferredSize(Dimension(600, 30))
		
		queryTitle3 = JLabel()
		queryTitle3.setText("Year:")
		queryTitle3.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel3 = JPanel()
		subPanel3.layout = FlowLayout(FlowLayout.LEFT)
		subPanel3.setMaximumSize(Dimension(800, 50))
		
		subPanel3.add(queryTitle3)
		subPanel3.add(comboBox2)
		
		self.midlePanel.add(subPanel3)
		self.comboList2.append(comboBox2)
		
		#################################################
		comboBox3 = JComboBox(self.listElementsCombo3)
		comboBox3.setPreferredSize(Dimension(600, 30))
		
		queryTitle4 = JLabel()
		queryTitle4.setText("Categorie:")
		queryTitle4.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel6 = JPanel()
		subPanel6.layout = FlowLayout(FlowLayout.LEFT)
		subPanel6.setMaximumSize(Dimension(800, 50))
		
		subPanel6.add(queryTitle4)
		subPanel6.add(comboBox3)
		
		self.midlePanel.add(subPanel6)
		self.comboList2.append(comboBox3)
		##################################################
		subPanel4 = JPanel()
		subPanel4.layout = FlowLayout(FlowLayout.CENTER)
		subPanel4.setMaximumSize(Dimension(800, 50))
		
		queryButton1 = JButton("Execute Query", actionPerformed = self.secondQueryExecuteEvent)
		queryButton2 = JButton("Generate html", actionPerformed = self.secondQueryExecuteHTMLEvent)
		queryButton3 = JButton("CSV Generator", actionPerformed = self.secondQueryExecuteCsv)
		
		
		subPanel4.add(queryButton1)
		subPanel4.add(queryButton2)
		subPanel4.add(queryButton3)
		self.midlePanel.add(subPanel4)
		#################################################
		
		subPanel5 = JPanel()
		subPanel5.layout = FlowLayout(FlowLayout.LEFT)
		subPanel5.setMaximumSize(Dimension(800, 50))
		
		queryResult1 = JLabel()
		queryResult1.setText("Result: ")
		queryResult1.setFont(Font("Arial", Font.BOLD, 15));
		
		self.queryResult3 = JLabel()
		self.queryResult3.setText("")
		self.queryResult3.setFont(Font("Arial", Font.BOLD, 15));
		
		
		subPanel5.add(queryResult1)
		subPanel5.add(self.queryResult3)
		self.midlePanel.add(subPanel5)
		
	def secondQueryEvent(self, event):
		
		self.midlePanel.removeAll()
		
		self.statusBarInfo.setText("Please wait as information is loaded...")
		
		self.frame.repaint()
		self.frame.setVisible(True)	
		
		JOptionPane.showMessageDialog(self.frame,"The Program need to load some data...")
							
		self.listElementsCombo1 = JDataAccess.getListEstablishment()
		self.listElementsCombo2 = JDataAccess.getListYears()
		self.listElementsCombo3 = JDataAccess.getListCategories()
						
		self.secondQuery()
		
		self.frame.repaint()
		self.frame.setVisible(True)
		self.statusBarInfo.setText("Ready!")
		
		
	def secondQueryExecuteEvent(self, event):
		
				
		self.listResult = JDataAccess.getSecondDefaultQuery(self.comboList2[0].getSelectedItem(), self.comboList2[1].getSelectedItem(), self.comboList2[2].getSelectedItem())
		self.queryResult3.setText(str(len(self.listResult)))
	
	def secondQueryExecuteHTMLEvent(self, event):
		
		listResultHtml = JDataAccess.getSecondDefaultQuery(self.comboList2[0].getSelectedItem(), self.comboList2[1].getSelectedItem(), self.comboList2[2].getSelectedItem())
		self.htmlModule.secondQuery(listResultHtml, self.comboList2[0].getSelectedItem(), self.comboList2[1].getSelectedItem(), self.comboList2[2].getSelectedItem())
	
	def secondQueryExecuteCsv(self, event):
		
		listResult = JDataAccess.getSecondDefaultQuery(self.comboList2[0].getSelectedItem(), self.comboList2[1].getSelectedItem(), self.comboList2[2].getSelectedItem())
		self.csvGenerator.secondQuery(listResult, len(listResult), self.comboList2[0].getSelectedItem(), self.comboList2[1].getSelectedItem(), self.comboList2[2].getSelectedItem())
		
		print "csv"
	
	def thirdQuery(self):
		
				
		queryTitle1 = JLabel()
		queryTitle1.setText("Search number of teachers in:")
		queryTitle1.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel1_1 = JPanel()
		subPanel1_1.setMaximumSize(Dimension(300, 50))
		subPanel1_1.add(queryTitle1)
		
		self.midlePanel.add(subPanel1_1)
		
		#################################################
		comboBox1 = JComboBox(self.listElementsCombo1)
		comboBox1.setPreferredSize(Dimension(600, 30))
		
		queryTitle2 = JLabel()
		queryTitle2.setText("Establishment:")
		queryTitle2.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel2 = JPanel()
		subPanel2.layout = FlowLayout(FlowLayout.LEFT)
		subPanel2.setMaximumSize(Dimension(800, 50))
		
		subPanel2.add(queryTitle2)
		subPanel2.add(comboBox1)
		
		self.midlePanel.add(subPanel2)
		self.comboList3.append(comboBox1)
		#################################################
		comboBox2 = JComboBox(self.listElementsCombo2)
		comboBox2.setPreferredSize(Dimension(600, 30))
		
		queryTitle3 = JLabel()
		queryTitle3.setText("Year:")
		queryTitle3.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel3 = JPanel()
		subPanel3.layout = FlowLayout(FlowLayout.LEFT)
		subPanel3.setMaximumSize(Dimension(800, 50))
		
		subPanel3.add(queryTitle3)
		subPanel3.add(comboBox2)
		
		self.midlePanel.add(subPanel3)
		self.comboList3.append(comboBox2)
		
		#################################################
		comboBox3 = JComboBox(self.listElementsCombo3)
		comboBox3.setPreferredSize(Dimension(600, 30))
		
		queryTitle4 = JLabel()
		queryTitle4.setText("Degree:")
		queryTitle4.setFont(Font("Arial", Font.BOLD, 15));
		
		subPanel6 = JPanel()
		subPanel6.layout = FlowLayout(FlowLayout.LEFT)
		subPanel6.setMaximumSize(Dimension(800, 50))
		
		subPanel6.add(queryTitle4)
		subPanel6.add(comboBox3)
		
		self.midlePanel.add(subPanel6)
		self.comboList3.append(comboBox3)
		##################################################
		subPanel4 = JPanel()
		subPanel4.layout = FlowLayout(FlowLayout.CENTER)
		subPanel4.setMaximumSize(Dimension(800, 50))
		
		queryButton1 = JButton("Execute Query", actionPerformed = self.thirdQueryExecuteEvent)
		queryButton2 = JButton("Generate html", actionPerformed = self.thirdQueryExecuteHTMLEvent)
		queryButton3 = JButton("CSV Generator", actionPerformed = self.thirdQueryExecuteCsv)
		
		subPanel4.add(queryButton1)
		subPanel4.add(queryButton2)
		subPanel4.add(queryButton3)
		
		self.midlePanel.add(subPanel4)
		#################################################
		
		subPanel5 = JPanel()
		subPanel5.layout = FlowLayout(FlowLayout.LEFT)
		subPanel5.setMaximumSize(Dimension(800, 50))
		
		queryResult1 = JLabel()
		queryResult1.setText("Result: ")
		queryResult1.setFont(Font("Arial", Font.BOLD, 15));
		
		self.queryResult4 = JLabel()
		self.queryResult4.setText("")
		self.queryResult4.setFont(Font("Arial", Font.BOLD, 15));
		
		
		subPanel5.add(queryResult1)
		subPanel5.add(self.queryResult4)
		self.midlePanel.add(subPanel5)
		
	def thirdQueryEvent(self, event):
		
		self.midlePanel.removeAll()
		
		self.statusBarInfo.setText("Please wait as information is loaded...")
		
		self.frame.repaint()
		self.frame.setVisible(True)	
		
		JOptionPane.showMessageDialog(self.frame,"The Program need to load some data...")
							
		self.listElementsCombo1 = JDataAccess.getListEstablishment()
		self.listElementsCombo2 = JDataAccess.getListYears()
		self.listElementsCombo3 = JDataAccess.getListDegrees()
						
		self.thirdQuery()
		
		self.frame.repaint()
		self.frame.setVisible(True)
		self.statusBarInfo.setText("Ready!")
		
		
	def thirdQueryExecuteEvent(self, event):
		
				
		self.listResult = JDataAccess.getThirdDefaultQuery(self.comboList3[0].getSelectedItem(), self.comboList3[1].getSelectedItem(), self.comboList3[2].getSelectedItem())
		self.queryResult4.setText(str(len(self.listResult)))
		
	def thirdQueryExecuteHTMLEvent(self, event):
		
		listResultHtml = JDataAccess.getThirdDefaultQuery(self.comboList3[0].getSelectedItem(), self.comboList3[1].getSelectedItem(), self.comboList3[2].getSelectedItem())
		self.htmlModule.thirdQuery(listResultHtml, self.comboList3[0].getSelectedItem(), self.comboList3[1].getSelectedItem(), self.comboList3[2].getSelectedItem())	
	
	def thirdQueryExecuteCsv(self, event):
		
		listResult = JDataAccess.getThirdDefaultQuery(self.comboList3[0].getSelectedItem(), self.comboList3[1].getSelectedItem(), self.comboList3[2].getSelectedItem())
		self.csvGenerator.thirdQuery(listResult, len(listResult), self.comboList3[0].getSelectedItem(), self.comboList3[1].getSelectedItem(), self.comboList3[2].getSelectedItem())
		
		print "csv"
